package bb.lanxing.model.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.garmin.fit.MonitoringReader;
//import com.orm.SugarRecord;
import com.orm.dsl.Ignore;
//import com.orm.dsl.Table;
//import com.orm.query.Select;
import com.umeng.analytics.pro.d;
import com.umeng.analytics.pro.z;
import bb.lanxing.model.WorkoutDatabaseHelper;
import bb.lanxing.model.json.ServerUser;
import bb.lanxing.model.json.TrackSegment;
import bb.lanxing.model.json.WorkoutExtraInfo;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Enums;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

public class WorkoutOther extends LitePalTableBase implements Parcelable, IWorkout, Cloneable {
    private static String COLUMNS_FOR_CHART = "id, altitude, speed, cadence, heartrate,power,latitude,longitude";
    private static String COLUMNS_FOR_MAP = "id,latitude,longitude,altitude,time";
    public static final Parcelable.Creator<WorkoutOther> CREATOR = new Parcelable.Creator<WorkoutOther>() {

        public WorkoutOther createFromParcel(Parcel parcel) {
            return new WorkoutOther(parcel);
        }


        public WorkoutOther[] newArray(int i) {
            return new WorkoutOther[i];
        }
    };
    @JSONField(name = "avg_cadence")
    private int avgCadence;
    @JSONField(name = "avg_heartrate")
    private int avgHeartrate;
    @JSONField(name = "powerAvg")
    private double avgPower;
    @JSONField(name = "avg_speed")
    private double avgSpeed;
    private int cadence;
    @JSONField(name = "cadence_source")
    private int cadenceSource;
    @JSONField(name = MonitoringReader.CALORIE_STRING)
    private int calorie;
    @JSONField(name = "category")
    private int category;
    @JSONField(name = "comment_count")
    private int commentCount;
    private String description;
    private double distance;
    @JSONField(name = "down_distance")
    private double downDistance;
    @JSONField(name = "down_duration")
    private long downDuration;
    private long duration;
    @JSONField(name = "elevation_gain")
    private float elevationGain;
    @JSONField(name = "elevation_loss")
    private float elevationLoss;
    @JSONField(name = "encoding_points")
    private String encodingPoints;
    @JSONField(name = "end_cadence")
    private int endCadence;
    @JSONField(name = d.q)
    private long endTime;
    @JSONField(name = "end_wheel")
    private int endWheel;
    @JSONField(name = "flat_distance")
    private double flatDistance;
    @JSONField(name = "flat_duration")
    private long flatDuration;
    @JSONField(name = "heart_source")
    private int heartSource;
    private int heartrate;
    @JSONField(name = "is_export")
    private boolean isExport;
    @JSONField(name = "is_like")
    @Ignore
    private int isLike;
    @JSONField(name = "is_valid")
    private boolean isValid;
    @JSONField(name = "like_count")
    private int likeCount;
    @JSONField(name = "loc_source")
    private int locSource;
    @JSONField(name = "map_hidden")
    private int mapHidden;
    @JSONField(name = "map_id")
    private int mapId;
    @JSONField(name = "is_segment")
    private int matchedSegments;
    @JSONField(name = "max_altitude")
    private double maxAltitude;
    @JSONField(name = "max_cadence")
    private int maxCadence;
    @JSONField(name = "max_grade")
    private int maxGrade;
    @JSONField(name = "max_heartrate")
    private int maxHeartrate;
    @JSONField(name = "powerMax")
    private double maxPower;
    @JSONField(name = "max_speed")
    private double maxSpeed;
    @JSONField(name = "max_wheel_rpm")
    private int maxWheelRevolution;
    @JSONField(name = "merge_record")
    private String mergeRecord;
    @JSONField(name = "min_grade")
    private int minGrade;
    private String poi;
    @Ignore
    private long pointCounts;
    @JSONField(name = "credits")
    private double points;
    @JSONField(name = "powerFTP")
    private double powerFTP;
    @JSONField(name = "powerIF")
    private double powerIF;
    @JSONField(name = "powerNP")
    private double powerNP;
    @JSONField(name = "powerSource")
    private int powerSource;
    @JSONField(name = "powerTSS")
    private double powerTSS;
    @JSONField(name = "powerVI")
    private double powerVI;
    @JSONField(name = "segments_ca")
    private String segmentCa;
    @JSONField(name = "segments_hr")
    private String segmentHr;
    @JSONField(name = "segment_index")
    private String segmentIndex;
    @JSONField(name = "segments_km")
    private String segmentKM;
    @JSONField(name = "segments_sport")
    private String segmentSport;
    @JSONField(name = "workout_id")
    private long serverId;
    private int sport;
    @JSONField(name = "start_cadence")
    private int startCadence;
    @JSONField(name = d.p)
    private long startTime;
    @JSONField(name = "start_wheel")
    private int startWheel;
    private int step;
    @JSONField(name = "threed_workout")
    private String threedWorkout;
    private String title;
    @JSONField(name = "segments")
    @Ignore
    private List<TrackSegment> trackSegments;
    @JSONField(name = "up_distance")
    private double upDistance;
    @JSONField(name = "up_duration")
    private long upDuration;
    @JSONField(name = "server_user")
    @Ignore
    private ServerUser user;
//    @JSONField(name = z.m)
    private long userId;
    private String uuid;


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public String getSlopeGradeMax() {
        return "";
    }

    public static WorkoutOther from(Workout workout) {
        WorkoutOther workoutOther = new WorkoutOther();
        workoutOther.setId(workout.getId());
        workoutOther.setServerId(workout.getServerId());
        workoutOther.setSport(workout.getSport());
        workoutOther.setStartTime(workout.getStartTime());
        workoutOther.setEndTime(workout.getEndTime());
        workoutOther.setDistance(workout.getDistance());
        workoutOther.setUserId(workout.getUserId());
        workoutOther.setDuration(workout.getDuration());
        workoutOther.setTitle(workout.getTitle());
        workoutOther.setMinGrade(workout.getMinGrade());
        workoutOther.setMaxGrade(workout.getMaxGrade());
        workoutOther.setElevationGain(workout.getElevationGain());
        workoutOther.setElevationLoss(workout.getElevationLoss());
        workoutOther.setUpDistance(workout.getUpDistance());
        workoutOther.setUpDuration(workout.getUpDuration());
        workoutOther.setDownDistance(workout.getDownDistance());
        workoutOther.setDownDuration(workout.getDownDuration());
        workoutOther.setFlatDistance(workout.getFlatDistance());
        workoutOther.setFlatDuration(workout.getFlatDuration());
        workoutOther.setUuid(workout.getUuid());
        workoutOther.setAvgSpeed(workout.getAvgSpeed());
        workoutOther.setMaxSpeed(workout.getMaxSpeed());
        workoutOther.setAvgCadence(workout.getAvgCadence());
        workoutOther.setMaxCadence(workout.getMaxCadence());
        workoutOther.setAvgHeartrate(workout.getAvgHeartrate());
        workoutOther.setStartCadence(workout.getStartCadence());
        workoutOther.setEndCadence(workout.getEndCadence());
        workoutOther.setStartWheel(workout.getStartWheel());
        workoutOther.setEndWheel(workout.getEndWheel());
        workoutOther.setMaxWheelRevolution(workout.getMaxWheelRevolution());
        workoutOther.setLocSource(workout.getLocSource());
        workoutOther.setHeartSource(workout.getHeartSource());
        workoutOther.setCadenceSource(workout.getCadenceSource());
        workoutOther.setEncodingPoints(workout.getEncodingPoints());
        workoutOther.setSegmentCa(workout.getSegmentCa());
        workoutOther.setSegmentHr(workout.getSegmentHr());
        workoutOther.setSegmentIndex(workout.getSegmentIndex());
        workoutOther.setSegmentKM(workout.getSegmentKM());
        workoutOther.setSegmentSport(workout.getSegmentSport());
        workoutOther.setThreedWorkout(workout.getThreedWorkout());
        workoutOther.setIsExport(workout.isExport());
        workoutOther.setCadence(workout.getCadence());
        workoutOther.setHeartrate(workout.getHeartrate());
        workoutOther.setCalorie(workout.getCalorie());
        workoutOther.setStep(workout.getStep());
        workoutOther.setPoints(workout.getCreditsInt());
        workoutOther.setCommentCount(workout.getCommentCount());
        workoutOther.setLikeCount(workout.getLikeCount());
        workoutOther.setIsLike(workout.isLike() ? 1 : 0);
        workoutOther.setValid(workout.isValid());
        workoutOther.setPoi(workout.getPoi());
        workoutOther.setCategory(workout.getCategory());
        workoutOther.setMergeRecord(workout.getMergeRecord());
        workoutOther.setDescription(workout.getDescription());
        return workoutOther;
    }

    public Workout to() {
        return (Workout) JSON.parseObject(JSON.toJSONString(this), Workout.class);
    }


    @NonNull
    public WorkoutOther clone() {
        try {
            return (WorkoutOther) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public ITrackPoint getSegmentDestination(TrackSegment trackSegment) {
        TrackPointOther trackPointOther = null;
        if (trackSegment == null) {
            return null;
        }
        int end = trackSegment.getEnd();
        SQLiteDatabase db = LitePal.getDatabase();//getSugarDb().getDB();
        Cursor rawQuery = db.rawQuery("SELECT " + COLUMNS_FOR_MAP + " FROM trackpointother WHERE workoutid = ? order by time asc limit?,1", new String[]{String.valueOf(this.id), String.valueOf((end - 1) / 2)});
        if (rawQuery.moveToFirst()) {
            trackPointOther = new TrackPointOther();
            trackPointOther.setId(rawQuery.getLong(0));
            trackPointOther.setLatitude(rawQuery.getDouble(1));
            trackPointOther.setLongitude(rawQuery.getDouble(2));
            trackPointOther.setAltitude(rawQuery.getDouble(3));
        }
        rawQuery.close();
        return trackPointOther;
    }

    public static WorkoutOther getByServerId(long j) {

        String serverId = String.valueOf(j);
        WorkoutOther find = LitePal.where("serverId=?", serverId).findFirst(WorkoutOther.class);

        return find;

//        List find = find(WorkoutOther.class, "server_id = ?", String.valueOf(j));
//        if (find.size() > 0) {
//            return (WorkoutOther) find.get(0);
//        }
//        return null;
    }

    @Override
    public List<ITrackPoint> getByWorkout() {

        List<TrackPointOther> find = LitePal.where("workoutId=?", this.id.toString()).order("time asc").find(TrackPointOther.class);



//        List find = TrackPointOther.find(TrackPointOther.class, "workout_id=? order by time asc", this.id.toString());
        ArrayList arrayList = new ArrayList(find.size());
        arrayList.addAll(find);
        return arrayList;
    }

    @Override
    public List<ITrackPoint> getByWorkoutForMap() {
        return getByWorkoutForMap(1);
    }

    @Override
    public List<ITrackPoint> getByWorkoutForWatermark() {
        return getByWorkoutForMap(1);
    }

    @Override
    public List<ITrackPoint> getByWorkoutForMap(int i) {
        SQLiteDatabase db = LitePal.getDatabase();
        LinkedList linkedList = new LinkedList();
        String str = i == 2 ? "sum_wheel" : "time";
        Cursor rawQuery = db.rawQuery("SELECT " + COLUMNS_FOR_MAP + " FROM trackpointother WHERE workoutid = ? order by " + str + " asc", new String[]{String.valueOf(this.id)});
        while (rawQuery.moveToNext()) {
            TrackPointOther trackPointOther = new TrackPointOther();
            trackPointOther.setId(rawQuery.getLong(0));
            trackPointOther.setLatitude(rawQuery.getDouble(1));
            trackPointOther.setLongitude(rawQuery.getDouble(2));
            trackPointOther.setAltitude(rawQuery.getDouble(3));
            if (CommonUtil.isPointValid(trackPointOther.getLatitude(), trackPointOther.getLongitude())) {
                linkedList.add(trackPointOther);
            }
        }
        rawQuery.close();
        return linkedList;
    }

    @Override
    public List<ITrackPoint> getByWorkoutForStartEndTimeSample(long j, long j2, int i, int i2) {
        Cursor cursor;
        int i3;
        long j3;
        long j4;
        SQLiteDatabase db = LitePal.getDatabase();
        LinkedList linkedList = new LinkedList();
        String str = i == 2 ? "sum_wheel" : "time";
        String str2 = "SELECT " + COLUMNS_FOR_MAP + " FROM trackpointother WHERE workoutid = ? AND time > ? AND time < ? order by " + str + " asc";
        Cursor rawQuery = db.rawQuery(str2, new String[]{String.valueOf(this.id), String.valueOf(j), String.valueOf(j2)});
        int count = rawQuery.getCount();
        if (count == 0) {
            j3 = (int) j;
            j4 = (int) j2;
            cursor = db.rawQuery(str2, new String[]{String.valueOf(this.id), String.valueOf(j3), String.valueOf(j4)});
            i3 = cursor.getCount();
        } else {
            cursor = rawQuery;
            i3 = count;
            j3 = j;
            j4 = j2;
        }
        if (i3 > i2) {
            cursor = db.rawQuery("SELECT " + COLUMNS_FOR_MAP + " FROM trackpointother WHERE workoutid = ? AND time > ? AND time < ? AND id % ? = 0 order by " + str + " asc", new String[]{String.valueOf(this.id), String.valueOf(j3), String.valueOf(j4), String.valueOf(((i3 - 1) / i2) + 1)});
        }
        while (cursor.moveToNext()) {
            TrackPointOther trackPointOther = new TrackPointOther();
            trackPointOther.setId(cursor.getLong(0));
            trackPointOther.setLatitude(cursor.getDouble(1));
            trackPointOther.setLongitude(cursor.getDouble(2));
            trackPointOther.setAltitude(cursor.getDouble(3));
            trackPointOther.setTime(cursor.getLong(4));
            if (CommonUtil.isPointValid(trackPointOther.getLatitude(), trackPointOther.getLongitude())) {
                linkedList.add(trackPointOther);
            }
        }
        cursor.close();
        return linkedList;
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChart() {
        return getTrackPointsForChart("");
    }

    public List<ITrackPoint> getTrackPointsForChart(String str) {
        SQLiteDatabase db = LitePal.getDatabase();
        LinkedList linkedList = new LinkedList();
        Cursor rawQuery = db.rawQuery("SELECT " + COLUMNS_FOR_CHART + " FROM trackpointother WHERE workoutid = ?" + str, new String[]{String.valueOf(this.id)});
        while (rawQuery.moveToNext()) {
            TrackPointOther trackPointOther = new TrackPointOther();
            trackPointOther.setId(rawQuery.getLong(0));
            trackPointOther.setAltitude(rawQuery.getDouble(1));
            trackPointOther.setSpeed(rawQuery.getFloat(2));
            trackPointOther.setCadence(rawQuery.getInt(3));
            trackPointOther.setHeartrate(rawQuery.getInt(4));
            trackPointOther.setPower(rawQuery.getDouble(5));
            trackPointOther.setLatitude(rawQuery.getDouble(6));
            trackPointOther.setLongitude(rawQuery.getDouble(7));
            linkedList.add(trackPointOther);
        }
        rawQuery.close();
        return linkedList;
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChartAbove0Speed() {
        return getTrackPointsForChart(" and speed > 0");
    }

    @Override
    public List<ITrackPoint> getTrackPointsForChartAbove30Heartrate() {
        return getTrackPointsForChart(" and heartrate > 30");
    }

    @Override
    public boolean upDatePointPower(List<Float> list, List<Float> list2) {
//        List list3 = Select.from(TrackPointOther.class).where("workout_id = ?", new String[]{String.valueOf(this.id)}).list();
        List<TrackPointOther> list3 = LitePal.where("workoutId=?", this.id.toString()).find(TrackPointOther.class);

        if (list3.size() > 0) {
            for (int i = 0; i < list3.size(); i++) {
                try {
                    ((TrackPointOther) list3.get(i)).setPower(list.get(i));
                    ((TrackPointOther) list3.get(i)).setDistance(list2.get(i));
                    ((TrackPointOther) list3.get(i)).save();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public long getCountByWorkout() {

        List<WorkoutOther> l = LitePal.where("workoutId=?", String.valueOf(this.id.toString())).find(WorkoutOther.class);
        long count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? ", new String[]{String.valueOf(this.id)}).count();
    }

    @Override
    public long getPointCounts() {
        if (this.pointCounts < 0) {
            this.pointCounts = getCountByWorkout();
        }
        return this.pointCounts;
    }

    public void setPointCounts(long j) {
        this.pointCounts = j;
    }

    @Override
    public long getHeartrateCountByWorkout() {

        List<WorkoutOther> l = LitePal.where("workoutId=? and heartrate > 0", this.id.toString()).find(WorkoutOther.class);
        long count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and heartrate > 0", new String[]{String.valueOf(this.id)}).count();
    }

    @Override
    public long getPowerCountByWorkout() {

        List<WorkoutOther> l = LitePal.where("workoutId=? and power > 0", this.id.toString()).find(WorkoutOther.class);
        long count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and power > 0", new String[]{String.valueOf(this.id)}).count();
    }

    @Override
    public long getCadenceCountByWorkout() {

        List<WorkoutOther> l = LitePal.where("workoutId=? and cadence > 0", this.id.toString()).find(WorkoutOther.class);
        long count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and cadence > 0", new String[]{String.valueOf(this.id)}).count();
    }

    @Override
    public long getCountInHeartrateSection(int min, int max) {
        long count = 0;
        List<TrackPointOther> l = null;

        if (min < 0) {
            min = 0;
        }
        if (max < 0) {
            l = LitePal.where("workoutId=? and heartrate > ?", this.id.toString(), String.valueOf(min)).find(TrackPointOther.class);
            count = l.size();
            return count;
//            return Select.from(TrackPointOther.class).where("workout_id = ? and heartrate > ?", new String[]{String.valueOf(this.id), String.valueOf(min)}).count();
        }

        l = LitePal.where("workoutId=? and heartrate > ? and heartrate <= ?", this.id.toString(), String.valueOf(min), String.valueOf(max)).find(TrackPointOther.class);
        count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and heartrate > ? and heartrate <= ?", new String[]{String.valueOf(this.id), String.valueOf(min), String.valueOf(max)}).count();
    }

    @Override
    public long getCountInPowerSection(int min, int max) {
        long count = 0;
        List<TrackPointOther> l = null;

        if (min < 0) {
            min = 0;
        }
        if (max < 0) {
            l = LitePal.where("workoutId=? and power > ?", this.id.toString(), String.valueOf(min)).find(TrackPointOther.class);
            count = l.size();
            return count;
//            return Select.from(TrackPointOther.class).where("workout_id = ? and power > ?", new String[]{String.valueOf(this.id), String.valueOf(min)}).count();
        }

        l = LitePal.where("workoutId=? and power > ? and power <= ?", this.id.toString(), String.valueOf(min), String.valueOf(max)).find(TrackPointOther.class);
        count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and power > ? and power <= ?", new String[]{String.valueOf(this.id), String.valueOf(min), String.valueOf(max)}).count();
    }

    @Override
    public long getCountInCadenceSection(int min, int max) {
        long count = 0;
        List<TrackPointOther> l = null;

        if (min < 0) {
            min = 0;
        }
        if (max < 0) {
            l = LitePal.where("workoutId=? and cadence > ?", this.id.toString(), String.valueOf(min)).find(TrackPointOther.class);
            count = l.size();
            return count;

//            return Select.from(TrackPointOther.class).where("workout_id = ? and cadence > ?", new String[]{String.valueOf(this.id), String.valueOf(min)}).count();
        }

        l = LitePal.where("workoutId=? and cadence > ? and cadence <= ?", this.id.toString(), String.valueOf(min), String.valueOf(max)).find(TrackPointOther.class);
        count = l.size();
        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? and cadence > ? and cadence <= ?", new String[]{String.valueOf(this.id), String.valueOf(min), String.valueOf(max)}).count();
    }

    @Override
    public long getCadenceSumByWorkout() {
        Cursor rawQuery = LitePal.getDatabase().rawQuery("select sum(cadence) from trackpointother where workoutid = ?", new String[]{String.valueOf(this.id)});
        long j = rawQuery.moveToFirst() ? rawQuery.getInt(0) : 0L;
        rawQuery.close();
        return j;
    }

    @Override
    public WorkoutExtraInfo getWorkoutExtraInfo() {
        WorkoutExtraInfo workoutExtraInfo;
        Cursor rawQuery = LitePal.getDatabase().rawQuery("select MAX(speed) as max_speed, MIN(altitude) as min_altitude, MAX(altitude) as max_altitude, MAX(power) as max_power, AVG(power) as avg_power from trackpointother where workoutid = ?", new String[]{String.valueOf(this.id)});
        if (rawQuery.moveToFirst()) {
            workoutExtraInfo = new WorkoutExtraInfo();
            workoutExtraInfo.setMaxSpeed(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("max_speed"))));
            workoutExtraInfo.setMaxAltitude(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("max_altitude"))));
            workoutExtraInfo.setMinAltitude(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("min_altitude"))));
            workoutExtraInfo.setMaxPower(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("max_power"))));
            workoutExtraInfo.setAvgPower(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("avg_power"))));
            workoutExtraInfo.setMaxPower(this.maxPower);
            workoutExtraInfo.setAvgPower(this.avgPower);
        } else {
            workoutExtraInfo = null;
        }
        rawQuery.close();
        return workoutExtraInfo;


    }

    public WorkoutExtraInfo getWorkoutExtraInfo(int i, int i2) {
        WorkoutExtraInfo workoutExtraInfo;
        Cursor rawQuery = LitePal.getDatabase().rawQuery("select MAX(speed) as max_speed, MIN(altitude) as min_altitude, MAX(altitude) as max_altitude from (select * from trackpointother where workoutid = ? limit ?,?)", new String[]{String.valueOf(this.id), String.valueOf(i), String.valueOf((i2 - i) + 1)});
        if (rawQuery.moveToFirst()) {
            workoutExtraInfo = new WorkoutExtraInfo();
            workoutExtraInfo.setMaxSpeed(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("max_speed"))));
            workoutExtraInfo.setMaxAltitude(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("max_altitude"))));
            workoutExtraInfo.setMinAltitude(Double.parseDouble(String.valueOf(rawQuery.getColumnIndex("min_altitude"))));
        } else {
            workoutExtraInfo = null;
        }
        rawQuery.close();
        return workoutExtraInfo;
    }

    public void resetLimitValues(int i, int i2) {
        Cursor rawQuery = LitePal.getDatabase().rawQuery("select MAX(cadence) as max_cadence, AVG(cadence) as avg_cadence, MAX(heartrate) as max_heartrate, AVG(heartrate) as avg_heartrate from (select * from trackpointother where workoutid = ? limit ?,?)", new String[]{String.valueOf(this.id), String.valueOf(i), String.valueOf((i2 - i) + 1)});
        if (rawQuery.moveToFirst()) {
            this.maxCadence = rawQuery.getColumnIndex("max_cadence");
            this.avgCadence = rawQuery.getColumnIndex("avg_cadence");
            this.maxHeartrate = rawQuery.getColumnIndex("max_heartrate");
            this.avgHeartrate = rawQuery.getColumnIndex("avg_heartrate");
        }
        rawQuery.close();
    }

    @Override
    public int getSport() {
        return this.sport;
    }

    public void setSport(int i) {
        this.sport = i;
    }

    @Override
    public long getStartTime() {
        return this.startTime;
    }

    public void setStartTime(long j) {
        this.startTime = j;
    }

    @Override
    public long getEndTime() {
        return this.endTime;
    }

    public void setEndTime(long j) {
        this.endTime = j;
    }

    @Override
    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double d) {
        this.distance = d;
    }

    @Override
    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long j) {
        this.duration = j;
    }

    @Override
    public long getUserId() {
        return this.userId;
    }

    public void setUserId(long j) {
        this.userId = j;
    }

    @Override
    public long getServerId() {
        return this.serverId;
    }

    public void setServerId(long j) {
        this.serverId = j;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    @Override
    public int getMinGrade() {
        return this.minGrade;
    }

    public void setMinGrade(int i) {
        this.minGrade = i;
    }

    @Override
    public int getMaxGrade() {
        return this.maxGrade;
    }

    public void setMaxGrade(int i) {
        this.maxGrade = i;
    }

    @Override
    public float getElevationGain() {
        return this.elevationGain;
    }

    public void setElevationGain(float f) {
        this.elevationGain = f;
    }

    @Override
    public float getElevationLoss() {
        return this.elevationLoss;
    }

    public void setElevationLoss(float f) {
        this.elevationLoss = f;
    }

    @Override
    public double getUpDistance() {
        return this.upDistance;
    }

    public void setUpDistance(double d) {
        this.upDistance = d;
    }

    @Override
    public long getUpDuration() {
        return this.upDuration;
    }

    public void setUpDuration(long j) {
        this.upDuration = j;
    }

    @Override
    public double getDownDistance() {
        return this.downDistance;
    }

    public void setDownDistance(double d) {
        this.downDistance = d;
    }

    @Override
    public long getDownDuration() {
        return this.downDuration;
    }

    public void setDownDuration(long j) {
        this.downDuration = j;
    }

    @Override
    public double getFlatDistance() {
        return this.flatDistance;
    }

    public void setFlatDistance(double d) {
        this.flatDistance = d;
    }

    @Override
    public long getFlatDuration() {
        return this.flatDuration;
    }

    public void setFlatDuration(long j) {
        this.flatDuration = j;
    }

    @Override
    public String getUuid() {
        return this.uuid;
    }

    public void setUuid(String str) {
        this.uuid = str;
    }

    @Override
    public double getAvgSpeed() {
        return this.avgSpeed;
    }

    public void setAvgSpeed(double d) {
        this.avgSpeed = d;
    }

    @Override
    public double getMaxSpeed() {
        return this.maxSpeed;
    }

    @Override
    public void setMaxSpeed(double d) {
        this.maxSpeed = d;
    }

    @Override
    public int getAvgCadence() {
        return this.avgCadence;
    }

    public void setAvgCadence(int i) {
        this.avgCadence = i;
    }

    @Override
    public int getMaxCadence() {
        return this.maxCadence;
    }

    public void setMaxCadence(int i) {
        this.maxCadence = i;
    }

    @Override
    public int getAvgHeartrate() {
        return this.avgHeartrate;
    }

    public void setAvgHeartrate(int i) {
        this.avgHeartrate = i;
    }

    @Override
    public int getMaxHeartrate() {
        return this.maxHeartrate;
    }

    public void setMaxHeartrate(int i) {
        this.maxHeartrate = i;
    }

    @Override
    public int getStartCadence() {
        return this.startCadence;
    }

    public void setStartCadence(int i) {
        this.startCadence = i;
    }

    @Override
    public int getEndCadence() {
        return this.endCadence;
    }

    public void setEndCadence(int i) {
        this.endCadence = i;
    }

    @Override
    public int getStartWheel() {
        return this.startWheel;
    }

    public void setStartWheel(int i) {
        this.startWheel = i;
    }

    @Override
    public int getEndWheel() {
        return this.endWheel;
    }

    public void setEndWheel(int i) {
        this.endWheel = i;
    }

    @Override
    public int getMaxWheelRevolution() {
        return this.maxWheelRevolution;
    }

    public void setMaxWheelRevolution(int i) {
        this.maxWheelRevolution = i;
    }

    @Override
    public int getLocSource() {
        return this.locSource;
    }

    public void setLocSource(int i) {
        this.locSource = i;
    }

    @Override
    public int getHeartSource() {
        return this.heartSource;
    }

    public void setHeartSource(int i) {
        this.heartSource = i;
    }

    @Override
    public int getCadenceSource() {
        return this.cadenceSource;
    }

    public void setCadenceSource(int i) {
        this.cadenceSource = i;
    }

    @Override
    public String getEncodingPoints() {
        return this.encodingPoints;
    }

    public void setEncodingPoints(String str) {
        this.encodingPoints = str;
    }

    @Override
    public String getSegmentIndex() {
        return this.segmentIndex;
    }

    public void setSegmentIndex(String str) {
        this.segmentIndex = str;
    }

    @Override
    public String getSegmentKM() {
        return this.segmentKM;
    }

    public void setSegmentKM(String str) {
        this.segmentKM = str;
    }

    @Override
    public String getSegmentSport() {
        return this.segmentSport;
    }

    public void setSegmentSport(String str) {
        this.segmentSport = str;
    }

    @Override
    public String getSegmentHr() {
        return this.segmentHr;
    }

    @Override
    public void setSegmentHr(String str) {
        this.segmentHr = str;
    }

    @Override
    public String getSegmentCa() {
        return this.segmentCa;
    }

    @Override
    public void setSegmentCa(String str) {
        this.segmentCa = str;
    }

    @Override
    public int getCadence() {
        return this.cadence;
    }

    @Override
    public void setCadence(int i) {
        this.cadence = i;
    }

    @Override
    public int getHeartrate() {
        return this.heartrate;
    }

    public void setHeartrate(int i) {
        this.heartrate = i;
    }

    @Override
    public int getCalorie() {
        return this.calorie;
    }

    public void setCalorie(int i) {
        this.calorie = i;
    }

    @Override
    public int getStep() {
        return this.step;
    }

    public void setStep(int i) {
        this.step = i;
    }

    public double getPoints() {
        return this.points;
    }

    public void setPoints(double d) {
        this.points = d;
    }

    @Override
    public boolean isValid() {
        return this.isValid;
    }

    public void setValid(boolean z) {
        this.isValid = z;
    }

    @Override
    public String getPoi() {
        return this.poi;
    }

    public void setPoi(String str) {
        this.poi = str;
    }

    @Override
    public int getCategory() {
        return this.category;
    }

    public void setCategory(int i) {
        this.category = i;
    }

    @Override
    public String getMergeRecord() {
        return this.mergeRecord;
    }

    public void setMergeRecord(String str) {
        this.mergeRecord = str;
    }

    @Override
    public int getPowerSource() {
        return this.powerSource;
    }

    @Override
    public void setPowerSource(int i) {
        this.powerSource = i;
    }

    @Override
    public double getMaxPower() {
        return this.maxPower;
    }

    @Override
    public void setMaxPower(double d) {
        this.maxPower = d;
    }

    @Override
    public double getAvgPower() {
        return this.avgPower;
    }

    @Override
    public void setAvgPower(double d) {
        this.avgPower = d;
    }

    @Override
    public double getPowerNP() {
        return this.powerNP;
    }

    @Override
    public void setPowerNP(double d) {
        this.powerNP = d;
    }

    @Override
    public double getPowerIF() {
        return this.powerIF;
    }

    @Override
    public void setPowerIF(double d) {
        this.powerIF = d;
    }

    @Override
    public double getPowerVI() {
        return this.powerVI;
    }

    @Override
    public void setPowerVI(double d) {
        this.powerVI = d;
    }

    @Override
    public double getPowerTSS() {
        return this.powerTSS;
    }

    @Override
    public void setPowerTSS(double d) {
        this.powerTSS = d;
    }

    @Override
    public double getPowerFTP() {
        return this.powerFTP;
    }

    @Override
    public void setPowerFTP(double d) {
        this.powerFTP = d;
    }

    @Override
    public String getDescription() {
        return this.description;
    }

    @Override
    public int getLikeCount() {
        return this.likeCount;
    }

    @Override
    public void setLikeCount(int i) {
        this.likeCount = i;
    }

    @Override
    public int getCommentCount() {
        return this.commentCount;
    }

    @Override
    public void setCommentCount(int i) {
        this.commentCount = i;
    }

    @Override
    public boolean hasMatchedSegment() {
        List<TrackSegment> list;
        return this.matchedSegments > 0 || ((list = this.trackSegments) != null && list.size() > 0);
    }

    public void setMatchedSegments(int i) {
        this.matchedSegments = i;
    }

    @Override
    public String getThreedWorkout() {
        return this.threedWorkout;
    }

    @Override
    public void setThreedWorkout(String str) {
        this.threedWorkout = str;
    }

    @Override
    public boolean isThreedWorkout() {
        return !TextUtils.isEmpty(this.threedWorkout);
    }

    @Override
    public boolean isExport() {
        return this.isExport;
    }

    public void setIsExport(boolean z) {
        this.isExport = z;
    }

    @Override
    public int getCreditsInt() {
        return (int) Math.round(this.points);
    }

    @Override
    public int getMapId() {
        return this.mapId;
    }

    @Override
    public Enums.UploadStatus getUploadStatus() {
        return Enums.UploadStatus.Uploaded;
    }

    @Override
    public void setMapId(int i) {
        this.mapId = i;
    }

    @Override
    public void checkPacePoint() {
        if (WorkoutDatabaseHelper.countPacePoint(this.uuid) > 0 || TextUtils.isEmpty(this.segmentKM)) {
            return;
        }
        try {
            savePacePoint();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void savePacePoint() throws JSONException {
        if (!TextUtils.isEmpty(this.segmentKM)) {
            JSONArray jSONArray = new JSONArray(this.segmentKM);
            ArrayList arrayList = new ArrayList(jSONArray.length());
            for (int i = 0; i < jSONArray.length(); i++) {
                arrayList.add(new PacePoint(jSONArray.getJSONObject(i), this));
            }
            WorkoutDatabaseHelper.savePacePoints(arrayList);
        }
    }

    public void setDescription(String str) {
        this.description = str;
    }

    @Override
    public List<TrackSegment> getTrackSegments() {
        return this.trackSegments;
    }

    @Override
    public void setIsLike(boolean z) {
        this.isLike = z ? 1 : 0;
    }

    public void setTrackSegments(List<TrackSegment> list) {
        this.trackSegments = list;
    }

    @Override
    public boolean isLike() {
        return this.isLike > 0;
    }

    public void setIsLike(int i) {
        this.isLike = i;
    }

    public ServerUser getUser() {
        return this.user;
    }

    public void setUser(ServerUser serverUser) {
        this.user = serverUser;
    }

    public double getMaxAltitude() {
        return this.maxAltitude;
    }

    public void setMaxAltitude(double d) {
        this.maxAltitude = d;
    }

    @Override
    public int getMapHidden() {
        return this.mapHidden;
    }

    public void setMapHidden(int i) {
        this.mapHidden = i;
    }

    public WorkoutOther() {
        this.locSource = 1;
        this.category = 0;
        this.pointCounts = -1L;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(this.sport);
        parcel.writeLong(this.startTime);
        parcel.writeLong(this.endTime);
        parcel.writeDouble(this.distance);
        parcel.writeLong(this.duration);
        parcel.writeLong(this.userId);
        parcel.writeLong(this.serverId);
        parcel.writeString(this.title);
        parcel.writeInt(this.minGrade);
        parcel.writeInt(this.maxGrade);
        parcel.writeFloat(this.elevationGain);
        parcel.writeFloat(this.elevationLoss);
        parcel.writeDouble(this.upDistance);
        parcel.writeLong(this.upDuration);
        parcel.writeDouble(this.downDistance);
        parcel.writeLong(this.downDuration);
        parcel.writeDouble(this.flatDistance);
        parcel.writeLong(this.flatDuration);
        parcel.writeString(this.uuid);
        parcel.writeDouble(this.avgSpeed);
        parcel.writeDouble(this.maxSpeed);
        parcel.writeInt(this.avgCadence);
        parcel.writeInt(this.maxCadence);
        parcel.writeInt(this.avgHeartrate);
        parcel.writeInt(this.maxHeartrate);
        parcel.writeInt(this.startCadence);
        parcel.writeInt(this.endCadence);
        parcel.writeInt(this.startWheel);
        parcel.writeInt(this.endWheel);
        parcel.writeInt(this.maxWheelRevolution);
        parcel.writeInt(this.locSource);
        parcel.writeInt(this.heartSource);
        parcel.writeInt(this.cadenceSource);
        parcel.writeString(this.encodingPoints);
        parcel.writeString(this.segmentIndex);
        parcel.writeString(this.segmentKM);
        parcel.writeString(this.segmentSport);
        parcel.writeString(this.segmentHr);
        parcel.writeString(this.segmentCa);
        parcel.writeInt(this.matchedSegments);
        parcel.writeString(this.threedWorkout);
        parcel.writeByte(this.isExport ? (byte) 1 : (byte) 0);
        parcel.writeInt(this.cadence);
        parcel.writeInt(this.heartrate);
        parcel.writeInt(this.calorie);
        parcel.writeInt(this.step);
        parcel.writeDouble(this.points);
        parcel.writeDouble(this.maxAltitude);
        parcel.writeInt(this.likeCount);
        parcel.writeInt(this.commentCount);
        parcel.writeByte(this.isValid ? (byte) 1 : (byte) 0);
        parcel.writeString(this.poi);
        parcel.writeInt(this.category);
        parcel.writeString(this.mergeRecord);
        parcel.writeInt(this.powerSource);
        parcel.writeDouble(this.maxPower);
        parcel.writeDouble(this.avgPower);
        parcel.writeDouble(this.powerNP);
        parcel.writeDouble(this.powerIF);
        parcel.writeDouble(this.powerVI);
        parcel.writeDouble(this.powerTSS);
        parcel.writeDouble(this.powerFTP);
        parcel.writeString(this.description);
        parcel.writeInt(this.isLike);
        parcel.writeParcelable(this.user, i);
        parcel.writeTypedList(this.trackSegments);
        parcel.writeLong(this.pointCounts);
        parcel.writeInt(this.mapHidden);
        parcel.writeInt(this.mapId);
    }

    protected WorkoutOther(Parcel parcel) {
        boolean z = true;
        this.locSource = 1;
        this.category = 0;
        this.pointCounts = -1L;
        this.sport = parcel.readInt();
        this.startTime = parcel.readLong();
        this.endTime = parcel.readLong();
        this.distance = parcel.readDouble();
        this.duration = parcel.readLong();
        this.userId = parcel.readLong();
        this.serverId = parcel.readLong();
        this.title = parcel.readString();
        this.minGrade = parcel.readInt();
        this.maxGrade = parcel.readInt();
        this.elevationGain = parcel.readFloat();
        this.elevationLoss = parcel.readFloat();
        this.upDistance = parcel.readDouble();
        this.upDuration = parcel.readLong();
        this.downDistance = parcel.readDouble();
        this.downDuration = parcel.readLong();
        this.flatDistance = parcel.readDouble();
        this.flatDuration = parcel.readLong();
        this.uuid = parcel.readString();
        this.avgSpeed = parcel.readDouble();
        this.maxSpeed = parcel.readDouble();
        this.avgCadence = parcel.readInt();
        this.maxCadence = parcel.readInt();
        this.avgHeartrate = parcel.readInt();
        this.maxHeartrate = parcel.readInt();
        this.startCadence = parcel.readInt();
        this.endCadence = parcel.readInt();
        this.startWheel = parcel.readInt();
        this.endWheel = parcel.readInt();
        this.maxWheelRevolution = parcel.readInt();
        this.locSource = parcel.readInt();
        this.heartSource = parcel.readInt();
        this.cadenceSource = parcel.readInt();
        this.encodingPoints = parcel.readString();
        this.segmentIndex = parcel.readString();
        this.segmentKM = parcel.readString();
        this.segmentSport = parcel.readString();
        this.segmentHr = parcel.readString();
        this.segmentCa = parcel.readString();
        this.matchedSegments = parcel.readInt();
        this.threedWorkout = parcel.readString();
        this.isExport = parcel.readByte() != 0;
        this.cadence = parcel.readInt();
        this.heartrate = parcel.readInt();
        this.calorie = parcel.readInt();
        this.step = parcel.readInt();
        this.points = parcel.readDouble();
        this.maxAltitude = parcel.readDouble();
        this.likeCount = parcel.readInt();
        this.commentCount = parcel.readInt();
        this.isValid = parcel.readByte() == 0 ? false : z;
        this.poi = parcel.readString();
        this.category = parcel.readInt();
        this.mergeRecord = parcel.readString();
        this.powerSource = parcel.readInt();
        this.maxPower = parcel.readDouble();
        this.avgPower = parcel.readDouble();
        this.powerNP = parcel.readDouble();
        this.powerIF = parcel.readDouble();
        this.powerVI = parcel.readDouble();
        this.powerTSS = parcel.readDouble();
        this.powerFTP = parcel.readDouble();
        this.description = parcel.readString();
        this.isLike = parcel.readInt();
        this.user = (ServerUser) parcel.readParcelable(ServerUser.class.getClassLoader());
        this.trackSegments = parcel.createTypedArrayList(TrackSegment.CREATOR);
        this.pointCounts = parcel.readLong();
        this.mapHidden = parcel.readInt();
        this.mapId = parcel.readInt();
    }
}
